package com.anthonybennis.blender.client.pages.keyboard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.anthonybennis.blender.client.DatabaseReader;
import com.anthonybennis.blender.client.pages.keyboard.TilePanel.PANEL;
import com.anthonybennis.blender.client.pages.tipofday.TipData;
import com.anthonybennis.blender.client.pages.tipofday.TipsDBClientBundle;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.TextResource;

/**
 * 
 * @author abennis
 */
public class TileDataFactory 
{
	public static final int FEATURE_NAME_INDEX = 0;
	public static final int SHORT_CUT_INDEX = 1;
	public static final int IMAGE_URL_INDEX = 2;
	public static final int DESCRIPTION_INDEX = 3;
	/**
	 * String[0] Feature Name.
	 * String[1] Short Cut
	 * String[2] Image URL
	 * String[3] wiki entry.
	 * 
	 * @param panel
	 * @return List of String[]
	 */
	protected ArrayList<String[]> createTileData(PANEL panel)
	{
		ArrayList<String[]> tileData = null;
		ShortcutsDBClientBundle shortcutsClientBundle = (ShortcutsDBClientBundle)GWT.create(ShortcutsDBClientBundle.class);
		TextResource textResource;
		
		switch (panel) 
		{
			case OBJECT:
			{
				textResource = shortcutsClientBundle.objectShortCutsDB();
				break;
			}
			case EDIT:
			{
				textResource = shortcutsClientBundle.editShortCutsDB();
				break;
			}
			case VIEW:
			{
				textResource = shortcutsClientBundle.navigationShortCutsDB();
				break;
			}
			default: // General
			{
				textResource = shortcutsClientBundle.generalShortCutsDB();
				break;
			}
		}
		
		tileData = loadDatabase(textResource);
		
		if (tileData != null)
		{
			java.util.Collections.sort(tileData, new ShortCutsComparator());
		}

		return tileData;
	}
	
	private static ArrayList<String[]> loadDatabase(TextResource textResource)
	{
		ArrayList<String[]> listOfTileData = new ArrayList<>();
		
		String allText = textResource.getText();
		allText = allText.replaceAll("(\\r|\\n)", "");
		
		String[] shortcutsArrayData = allText.split(DatabaseReader.TERMINATION_CHARACTER);
		
		for (String data : shortcutsArrayData) 
		{
			String[] tokenisedData = data.split(DatabaseReader.SEPERATOR);
			listOfTileData.add(new String[]{tokenisedData[0],tokenisedData[1],"",tokenisedData[2]});
		}
		
		
		return listOfTileData;
	}
}